﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Controls;
using GSSoftSL.Utility;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;

namespace GSSoftSL.ViewModel.HocVienVM
{
    public class HocVienEditViewModel : ViewModelBase
    {
        private GSSoftContext _dc;
        public GSSoftContext DC
        {
            get { return _dc; }
            set { _dc = value; }
        }

        /// <summary>
        /// Initializes a new instance of the HocVienEditViewModel class.
        /// </summary>
        public HocVienEditViewModel()
        {
            _dc = ServiceLocator.Current.GetInstance<DomainContext>("HocVien") as GSSoftContext;
            MainViewModel = ServiceLocator.Current.GetInstance<HocVienMainViewModel>("HocVien");
            MainViewModel.IsBusy = true;
            _dc.Load<Truong>(_dc.GetTruongsQuery(), LoadBehavior.RefreshCurrent, LoadTruong_Completed, null);
        }

        private void LoadTruong_Completed(LoadOperation<Truong> loadTruong)
        {
            Truongs = loadTruong.Entities;
            MainViewModel.IsBusy = false;
        }

        private HocVienMainViewModel _mainViewModel;
        public HocVienMainViewModel MainViewModel
        {
            get { return _mainViewModel; }
            set { _mainViewModel = value; }
        }
        
        private HocVien _hocVien;
        public HocVien HocVien
        {
            get
            {
                return _hocVien;
            }
            set
            {
                if (_hocVien == value)
                {
                    return;
                }
                _hocVien = value;
                RaisePropertyChanged("HocVien");
            }
        }
        
        private IEnumerable<Truong> _truongs = null;
        public IEnumerable<Truong> Truongs
        {
            get
            {
                return _truongs;
            }
            set
            {
                if (_truongs == value)
                {
                    return;
                }
                _truongs = value;
                RaisePropertyChanged("Truongs");
            }
        }

        private RelayCommand<bool> _saveCommand;
        public RelayCommand<bool> SaveCommand
        {
            get
            {
                return _saveCommand
                       ?? (_saveCommand = new RelayCommand<bool>(
                                              b =>
                                                  {
                                                      if (_hocVien == null || _dc.IsSubmitting || !_dc.HasChanges)
                                                          return;
                                                      ConfirmWindow.CreateNew("Bạn có muốn lưu?", Save);
                                                      //Save();
                                                  },
                                              b => !(_hocVien == null || _dc.IsSubmitting)));
            }
        }

        public void Save()
        {
            if (_hocVien == null || _dc.IsSubmitting)
                return;
            if (_hocVien.EntityState == EntityState.New || _hocVien.EntityState == EntityState.Detached)
            {
                _hocVien.NguoiTao = _hocVien.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
                _hocVien.NgayTao = _hocVien.NgayCapNhat = DateTime.Now;
                _dc.HocViens.Add(_hocVien);
            }
            else if (_hocVien.EntityState == EntityState.Modified)
            {
                _hocVien.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
                _hocVien.NgayCapNhat = DateTime.Now;
                //_dc.HocViens.Attach(_hocVien);
            }
            MainViewModel.IsBusy = true;
            _dc.SubmitChanges(Save_Completed, null);
        }

        private RelayCommand _cancelCommand;
        public RelayCommand CancelCommand
        {
            get
            {
                return _cancelCommand
                    ?? (_cancelCommand = new RelayCommand(
                                          () =>
                                          {
                                              if (_dc.HasChanges)
                                                  _dc.RejectChanges();
                                              HocVien = null;
                                          }));
            }
        }

        private void Save_Completed(SubmitOperation obj)
        {
            MainViewModel.IsBusy = false;
            //RaisePropertyChanged("HocVien");
        }

        private DataFormMode _mode = DataFormMode.ReadOnly;
        public DataFormMode Mode
        {
            get
            {
                return _mode;
            }
            set
            {
                if (_mode == value)
                {
                    return;
                }
                _mode = value;
                RaisePropertyChanged("Mode");
            }
        }

        
        private bool _isEnabled = false;
        public bool IsEnabled
        {
            get
            {
                return _isEnabled;
            }
            set
            {
                if (_isEnabled == value)
                {
                    return;
                }
                _isEnabled = value;
                RaisePropertyChanged("IsEnabled");
            }
        }

    }
}